package com.simulation.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simulation.common.utils.SecurityUtils;
import com.simulation.web.dto.TechCommentDTO;
import com.simulation.web.domain.TechComment;
import com.simulation.web.mapper.CommentMapper;
import com.simulation.web.service.CommentService;
import com.simulation.web.service.ReplyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class CommentServiceImpl implements CommentService {

    private final CommentMapper commentMapper;

    private final ReplyService replyService;

    @Override
    public PageInfo getPage(TechCommentDTO techCommentDTO) {
        PageHelper.startPage(techCommentDTO.getPageNum(), techCommentDTO.getPageSize());
        List<TechComment> commentList = commentMapper.getPage(techCommentDTO);
        return new PageInfo<>(commentList);
    }

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @Override
    public List<TechComment> listAll() {
    	return commentMapper.listAll();
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @Override
    public TechComment getById(Long id) {
    	return commentMapper.getById(id);
    }
	
    /**
     * 新增，插入所有字段
     *
     * @param techComment 新增的记录
     * @return 返回影响行数
     */
    @Override
    public int insert(TechComment techComment) {
        techComment.setCommentTime(new Date());
    	return commentMapper.insert(techComment);
    }
	
    /**
     * 新增，忽略null字段
     *
     * @param techComment 新增的记录
     * @return 返回影响行数
     */
    @Override
    public int insertIgnoreNull(TechComment techComment) {
        techComment.setCommentTime(new Date());
        techComment.setUserId(SecurityUtils.getUserId());
    	return commentMapper.insertIgnoreNull(techComment);
    }
	
    /**
     * 修改，修改所有字段
     *
     * @param techComment 修改的记录
     * @return 返回影响行数
     */
    @Override
    public int update(TechComment techComment) {
    	return commentMapper.update(techComment);
    }
	
    /**
     * 修改，忽略null字段
     *
     * @param techComment 修改的记录
     * @return 返回影响行数
     */
    @Override
    public int updateIgnoreNull(TechComment techComment) {
    	return commentMapper.updateIgnoreNull(techComment);
    }
	
    /**
     * 删除记录
     *
     * @param idList 待删除的记录
     * @return 返回影响行数
     */
    @Override
    public int delete(List<Long> idList) {
        // 删除当前评论下的所有回复
        idList.stream().forEach(id -> {
            replyService.deleteByCommentId(id);
        });
    	return commentMapper.delete(idList);
    }
}